//
//  BatteryViewController.m
//  phoneTool
//
//  Created by HappyMilk 02 on 2017/8/1.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "BatteryViewController.h"
#import "LoginHeader.h"

@interface BatteryViewController ()

@property (weak, nonatomic) IBOutlet UIView *view_all;
@property (weak, nonatomic) IBOutlet UILabel *lab_shengyu;

@property (weak, nonatomic) IBOutlet UILabel *lab_time;


@end

@implementation BatteryViewController
- (IBAction)popvc:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(CGFloat)getBatteryQuantity
{
    return [[UIDevice currentDevice] batteryLevel];
}
-(UIDeviceBatteryState)getBatteryStauts
{
    return [UIDevice currentDevice].batteryState;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //232 5
    
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
    
    UIView *view = [[UIView alloc] initWithFrame:(CGRect){5,242-5,160-10,0}];
    view.backgroundColor = HEXCOLOR(0x58B05E);
    [_view_all addSubview:view];
    
    CGFloat number = [self getBatteryQuantity];
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        view.frame = (CGRect){5,242-5,160-10,-number*232};
    } completion:^(BOOL finished) {
        
    }];
    
    if (!IS_CHINESE) {
        self.lab_time.text = [NSString stringWithFormat:@"It could be used for %.f minutes",number*1060];
    }else{
        self.lab_time.text = [NSString stringWithFormat:@"预计可使用 %.f 分钟",number*1060];
    }
    
    
    switch ([self getBatteryStauts]) {

        case UIDeviceBatteryStateCharging:
            self.lab_shengyu.text = IS_CHINESE?@"正在充电":@"Charging";
            break;
        default:
            self.lab_shengyu.text = [NSString stringWithFormat:@"%%%.f",number*100];
            break;
    }
}
-(void)batteryStateChanged:(id)sender{

    switch ([self getBatteryStauts]) {
        case UIDeviceBatteryStateCharging:
            self.lab_shengyu.text = IS_CHINESE?@"正在充电":@"Charging";
            break;
            
        default:
    
            self.lab_shengyu.text = [NSString stringWithFormat:@"%%%.f",[self getBatteryQuantity]*100];
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
